package com.yc.pre.config.security;

import com.yc.pre.entity.bean.PreRole;
import com.yc.pre.entity.bean.PreSignUser;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @ClassName PreSignUserDetails
 * @Description 继承UserDetails，实现自定义UserDetails
 * @Date 2021/02/08 18:02:39
 * @Created by ccc-j
 * @email ccc-ju@outlook.com
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PreSignUserDetails implements UserDetails {

    private PreSignUser preSignUser;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        PreRole role = preSignUser.getPreRole();
        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
        authorities.add(new SimpleGrantedAuthority(role.getRoleName()));
        return authorities;
    }

    @Override
    public String getPassword() {
        return preSignUser.getPassword();
    }

    @Override
    public String getUsername() {
        return preSignUser.getUserName();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
